{layout name="layout/header" /}
<link href="root/css/plugins/footable/footable.core.css" rel="stylesheet">

<link href="root/css/plugins/switchery/switchery.css" rel="stylesheet">
<script src="root/js/plugins/switchery/switchery.js"></script>
<!--<link href="root/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">-->

<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5><span class="text-danger">{$title}</span> 资金流水</h5>
                </div>
                <div class="ibox-content">
                    <p>
                        <!--<a type="button" class="btn btn-primary btn-outline" data-toggle="modal" href="#modal-form">添加</a>-->
                        <a href="{:url('add',['pid'=>$pid])}" class="btn btn-danger btn-outline refresh">新增</a>
                        <button type="button" class="btn btn-primary btn-outline refresh">刷新</button>
                    </p>
                </div>
                <div class="ibox-content">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>项目</th>
                                <th>款项</th>
                                <th>价格</th>
                                <th>类型</th>
                                <th>备注</th>
                                <th>收入/支出时间</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        {volist name="data.list" id="vo"}
                            <tr pid="{$vo.id}">
                                <td>{$vo.pname}</td>
                                <td>{$vo.cash_name}</td>
                                <td>{$vo.price}</td>
                                <td>
                                    {switch name="$vo.type"}
                                    {case value="1"}<span class="text-success">收入</span>{/case}
                                    {case value="2"}<span class="text-danger">支出</span>{/case}
                                    {/switch}
                                </td>
                                <td><input type="text" class="form-control update_text" data-field="remark" value="{$vo.remark}"></td>
                                <td>{$vo.time|date="Y-m-d",###}</td>
                                <td>{$vo.addtime|date="Y-m-d H:i",###}</td>
                                <td>
                                    <a type="button" class="btn btn-primary btn-sm " href="{:url('update',['id'=>$vo.id,'pid'=>$pid])}">编辑</a>
                                </td>
                            </tr>
                        {/volist}
                        </tbody>
                        <tfoot>
                        <tr>
                            <td colspan="12">
                                {$data.page}
                            </td>
                        </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>


<script>
    $(document).ready(function() {
        var pid = {$pid};
        var elem = document.querySelectorAll('.js-switch');
        elem.forEach(function(i,index){
            var a= "a" +index;
            a = new Switchery(i, {
                hours: '#1AB394'
            });
        });
        //新添扩展字段
        $('#sub').click(function () {
            // var name = $('#name').val().trim();
            // if(name == '' ){
            //     layer.msg('名称不能为空!');
            //     return false;
            // }

            var cash_type_id = $("#cash_type_id").val();
            var remark = $("#remark").val().trim();
            var price = $("#price").val().trim();
            var type = $('#type').val();
            var reg = /^\d+$/;
            if(!(/^\d+$/.test(price))){
                layer.msg('数量格式不正确');
                return false;
            }
            $.ajax({
                url:"{:url('ajax_add')}",
                type:"post",
                data:{pid:pid,cash_type_id:cash_type_id,remark:remark,price:price,type:type},
                dataType:"json",
                success:function (data) {
                    if(data.code == 1){
                        layer.msg(data.msg);
                        setTimeout(function(){
                            window.location.reload();
                        },1500);
                        return false;
                    }else {
                        layer.msg(data.msg);
                        return false;
                    }
                }
            });
        });

        //列表页修改
        $(".update_text").blur(function () {
            var field = $(this).attr('data-field');
            var id = $(this).parent().parent().attr('pid');
            var value = $(this).val().trim();
            update(id,field,value);
        });
        $(".update_checkbox").change(function () {
            var field = $(this).attr('data-field');
            var value = $(this).is(':checked')?1:0;
            var id = $(this).parent().parent().attr('pid');
            update(id,field,value);
        });
        function update(id,field,value) {
            $.ajax({
                url:"{:url('ajax_update')}",
                type:'post',
                dataType:'json',
                data:{field:field,value:value,id:id},
                success:function (data) {
                    console.log(data.msg);
                    if(data.code != 1){
                        layer.msg(data.msg);
                    }
                }
            })
        }

        //产品删除
        $('.product_del').click(function(){
            var id = $(this).parent().parent().attr('pid');
            var $this = $(this);

            layer.confirm('确定删掉吗?', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    url:"{:url('ajax_del')}",
                    type:'post',
                    data:{id:id},
                    dataType:'json',
                    success:function (data) {
                        if(data.code==1){
                            layer.msg(data.msg);
                            $this.parent().parent().remove();
                        }else {
                            layer.msg(data.msg);
                        }
                    }
                })
            }, function(){

            });

        });
    });

</script>
